﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.DAL;
using OneMile.Common;
using OneMile.Entity.Model;
using OneMile.BLL;

namespace YGWebMobileApi.APIs
{
    [MobileAPI(Code = "MC0402", Name = "用户获取当前店铺未过期的抵用券信息")]
    public class UGetCouponListByUserIdAndShopsId : BaseAPI, IProcessMessage
    {
        private BLLUCoupon bllUCoupon;
        public BLLUCoupon BllUCoupon
        {
            get
            {
                if (bllUCoupon == null)
                    bllUCoupon = BLLFactory.getInstance().CreateUCouponBLL();
                return bllUCoupon;
            }
        }

        MobileRspMsg IProcessMessage.ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);

                string UserId = reqMsg.Body.GetFieldValue("UserId");
                string ShopsId = reqMsg.Body.GetFieldValue("ShopsId");
                var flll = reqMsg.Body.GetFieldList("CommodityIds");
                List<String> idList = null;
                if (flll != null)
                {
                    if (flll.Childs.Length != 0)
                    {
                        idList = new List<string>();
                        foreach (var f in flll.GetChilds("CommodityId"))
                        {
                            idList.Add(f.Value);
                        }
                    }
                }
                if (String.IsNullOrEmpty(UserId) || string.IsNullOrEmpty(ShopsId))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }
                List<ECoupon> list = (List<ECoupon>)BllUCoupon.GetCouponListByUserIdAndShopId(UserId, ShopsId, idList);

                if (list != null)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        var fll = rspMsg.Body.AddFieldList("Coupon");
                        fll.AddField("CouponId", list[i].CouponId.TString());
                        fll.AddField("CouponPrice", list[i].CouponPrice.TString());
                        fll.AddField("CouponName", list[i].CouponName.TString());
                        fll.AddField("StartDate", list[i].StartDate.TString());
                        fll.AddField("EndDate", list[i].EndDate.TString());
                        fll.AddField("IsUsed", list[i].IsUsed.TString());
                        fll.AddField("UsedTime", list[i].UsedTime.ToString());
                    }

                    rspMsg.Head.RspCode = MessageCode.Success.Code;
                    rspMsg.Head.RspMsg = MessageCode.Success.Msg;
                }
                else
                {
                    rspMsg.Head.RspCode = MessageCode.Result_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Result_Empty.Msg;
                }

                return rspMsg;
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("GetCouponListByUserIdAndShopId" + e);
                throw e;
            }
        }
    }
}